#ifndef __LINKLIST_H_
#define __LINKLIST_H_

#include <iostream>

template<typename T>
class LinkList
{
public:
    LinkList();
    void InsertTail(const T& element);
    template<typename U>
    friend std::ostream &operator<<(std::ostream& os,const LinkList<U>& list);
    void show();
private:
    struct Node
    {
        Node(const T& element)
        {
            data = element;
            next = nullptr;
        }
        T data; //数据域
        Node* next;
    };
    Node* head;
    int size ;
};


template<typename T>
LinkList<T>::LinkList()
{
    //模板支持匿名对象
    head = new Node(T());
    size = 0;
}

template <typename T>
inline void LinkList<T>::InsertTail(const T &element)
{
    // 遍历指针
    Node *travelPoint = head;
    while (travelPoint->next != nullptr)
        travelPoint = travelPoint->next;
    travelPoint->next = new Node(element); //()是初始化
    size++;
}


template <typename T>
void LinkList<T>::show()
{
    std::cout << "size:" << size << std::endl;
    // 遍历指针
    Node *travelPoint = head->next; // 头结点不需要输出
    while (travelPoint != nullptr)
    {
        std::cout << travelPoint->data << " ";
        travelPoint = travelPoint->next;
    }
    std::cout << std::endl;
}

template<typename U>
inline std::ostream &operator<<(std::ostream& os,const LinkList<U>& list)
{
    std::cout<<"size:"<<list.size<<std::endl;
    //遍历指针
    //typenama : 声明后面跟着的模板是一个类型 
    typename LinkList<U>::Node* travelPoint = list.head->next;
    while(travelPoint != nullptr)
    {
        std::cout<<travelPoint->data<<" ";
        travelPoint = travelPoint->next;
    }
    std::cout<<std::endl;
    return os;
}

#endif